package com.xquant.pricing.systerm.controller;

import cn.hutool.core.map.MapUtil;
import com.jcraft.jsch.*;
import com.xquant.common.engine.xPP.cxf.XQuantPPConfig;
import com.xquant.common.engine.xPP.cxf.XQuantPPService;
import com.xquant.common.engine.xPP.cxf.XQuantPPStubFactory;
import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.pricing.systerm.entity.ServeStatusVo;
import com.xquant.pricing.utils.CommResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 * @author: zip.li
 * @date: Created in 2022/3/14 16:17
 * @Description: 监控计算节点
 */

@RestController
@RequestMapping("/view")
public class SysVisulationController {
    private static Logger logger = LoggerFactory.getLogger(SysVisulationController.class);
    public static final String CPU_MEM_SHELL = "top -b -n 1";
    public static final String FILES_SHELL = "df -hl";
    public static final String[] COMMANDS = {CPU_MEM_SHELL, FILES_SHELL};
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static com.jcraft.jsch.Session session;

    /**
     * 连接到指定的HOST
     *
     * @param host   远程连接的主机IP
     * @return isConnect
     * @throws JSchException JSchException
     */
    public static void SysVisulationController( String host ) throws JSchException {
        JSch jsch = new JSch();
        try {
            try {
                session.disconnect();
            } catch (Exception e) {
                System.out.println("未连接！");
            }
            session = jsch.getSession("root", host,22);
            session.setPassword("xQuant#7.4");

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();

        } catch (JSchException e) {
            e.printStackTrace();
            logger.error("connect error !");

        }
    }

    /**
     * 远程连接Linux 服务器 执行相关的命令
     *
     * @param commands 执行的脚本
     * @return 最终命令返回信息
     */
    public static Map<String, String> runDistanceShell(String[] commands) {

        Map<String, String> map = new HashMap<>();
        StringBuilder stringBuffer;

        BufferedReader reader = null;
        Channel channel = null;
        try {
            for (String command : commands) {
                stringBuffer = new StringBuilder();
                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(command);

                channel.setInputStream(null);
                ((ChannelExec) channel).setErrStream(System.err);

                channel.connect();
                InputStream in = channel.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                String buf;
                while ((buf = reader.readLine()) != null) {

                    //舍弃PID 进程信息
                    if (buf.contains("PID")) {
                        break;
                    }
                    stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
                }
                //每个命令存储自己返回数据-用于后续对返回数据进行处理
                map.put(command, stringBuffer.toString());
            }
        } catch (IOException | JSchException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return map;
    }

    /**
     * 直接在本地执行 shell
     *
     * @param commands 执行的脚本
     * @return 执行结果信息
     */
    public static Map<String, String> runLocalShell(String[] commands) {
        Runtime runtime = Runtime.getRuntime();

        Map<String, String> map = new HashMap<>();
        StringBuilder stringBuffer;

        BufferedReader reader;
        Process process;
        for (String command : commands) {
            stringBuffer = new StringBuilder();
            try {
                process = runtime.exec(command);
                InputStream inputStream = process.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                String buf;
                while ((buf = reader.readLine()) != null) {
                    //舍弃PID 进程信息
                    if (buf.contains("PID")) {
                        break;
                    }
                    stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
                }

            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            //每个命令存储自己返回数据-用于后续对返回数据进行处理
            map.put(command, stringBuffer.toString());
        }
        return map;
    }

    /**
     * 处理 shell 返回的信息
     * <p>
     * 具体处理过程以服务器返回数据格式为准
     * 不同的Linux 版本返回信息格式不同
     *
     * @param result shell 返回的信息
     * @return 最终处理后的信息
     */
    private static String disposeResultMessage(Map<String, String> result) {

        StringBuilder buffer = new StringBuilder();

        for (String command : COMMANDS) {
            String commandResult = result.get(command);
            if (null == commandResult) continue;

            if (command.equals(CPU_MEM_SHELL)) {
                String[] strings = commandResult.split(LINE_SEPARATOR);
                //将返回结果按换行符分割
                for (String line : strings) {
                    line = line.toUpperCase();//转大写处理

                    //处理CPU Cpu(s): 10.8%us,  0.9%sy,  0.0%ni, 87.6%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
                    if (line.split(":")[0].indexOf("CPU(S):") > -1) {
                        String cpuStr = "CPU 用户使用占有率:";
                        try {
                            cpuStr += line.split(":")[1].split(",")[0].replace("US", "");
                        } catch (Exception e) {
                            e.printStackTrace();
                            cpuStr += "计算过程出错";
                        }
                        buffer.append(cpuStr).append(LINE_SEPARATOR);

                        //处理内存 Mem:  66100704k total, 65323404k used,   777300k free,    89940k buffers
                    } else if (line.split(":")[0].indexOf("MEM") > -1) {
                        String memStr = "";
                        try {
                            memStr += line.split(":")[1]
                                    .replace("TOTAL", "")
                                    .replace("USED", "")
                                    .replace("FREE", "")
                                    .replace("BUFF/CACHE", "");

                        } catch (Exception e) {
                            e.printStackTrace();
                            memStr += "计算过程出错";
                            buffer.append(memStr).append(LINE_SEPARATOR);
                            continue;
                        }
                        buffer.append(memStr).append(LINE_SEPARATOR);

                    } else if (line.split(":")[0].indexOf("SWAP") > -1) {
                        String swapStr = "交换:";
                        try {
                            swapStr += line.split(":")[1]
                                    .replace("TOTAL", "")
                                    .replace("USED.", ",")
                                    .replace("FREE", "")
                                    .replace("AVAIL MEM", "");

                        } catch (Exception e) {
                            e.printStackTrace();
                            swapStr += "计算过程出错";
                            buffer.append(swapStr).append(LINE_SEPARATOR);
                            continue;
                        }
                        buffer.append(swapStr).append(LINE_SEPARATOR);
                    }
                }
            } else if (command.equals(FILES_SHELL)) {
                //处理系统磁盘状态
                buffer.append("系统磁盘状态:");
                try {
                    buffer.append(disposeFilesSystem(commandResult)).append(LINE_SEPARATOR);
                } catch (Exception e) {
                    e.printStackTrace();
                    buffer.append("计算过程出错").append(LINE_SEPARATOR);
                }
            }
        }
        return buffer.toString();
    }

    /**
     * Filesystem            Size  Used Avail Use% Mounted on
     * /dev/sda3             442G  327G   93G  78% /
     * tmpfs                  32G     0   32G   0% /dev/shm
     * /dev/sda1             788M   60M  689M   8% /boot
     * /dev/md0              1.9T  483G  1.4T  26% /ezsonar
     *
     * @param commandResult 处理系统磁盘状态shell执行结果
     * @return 处理后的结果
     */
    private static String disposeFilesSystem(String commandResult) {
        String[] strings = commandResult.split(LINE_SEPARATOR);

        // final String PATTERN_TEMPLATE = "([a-zA-Z0-9%_/]*)\\s";
        int size = 0;
        int used = 0;
        for (int i = 0; i < strings.length - 1; i++) {
            if (i == 0) continue;

            int temp = 0;
            for (String s : strings[i].split("\\b")) {
                if (temp == 0) {
                    temp++;
                    continue;
                }
                if (!s.trim().isEmpty()) {
                    if (temp == 1) {
                        size += disposeUnit(s);
                        temp++;
                    } else {
                        used += disposeUnit(s);
                        temp = 0;
                    }
                }
            }
        }
        return new StringBuilder().append("大小 ").append(size).append("G , 已使用").append(used).append("G ,空闲")
                .append(size - used).append("G").toString();
    }

    //处理系统磁盘状态

    /**
     * 处理单位转换
     * K/KB/M/T 最终转换为G 处理
     *
     * @param s 带单位的数据字符串
     * @return 以G 为单位处理后的数值
     */
    private static int disposeUnit(String s) {

        try {
            s = s.toUpperCase();
            String lastIndex = s.substring(s.length() - 1);
            String num = s.substring(0, s.length() - 1);
            int parseInt = Integer.parseInt(num);
            if (lastIndex.equals("G")) {
                return parseInt;
            } else if (lastIndex.equals("T")) {
                return parseInt * 1024;
            } else if (lastIndex.equals("M")) {
                return parseInt / 1024;
            } else if (lastIndex.equals("K") || lastIndex.equals("KB")) {
                return parseInt / (1024 * 1024);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    @Autowired
    private XQuantPPService ppService;

    @Autowired
    public XQuantPPConfig xQuantPPConfig;

    @RequestMapping("/getServeLog.action")
    public Map getServeLog(@RequestParam Map<String, Object> param) throws Exception {
        HashMap<String, String> serveMsg = new HashMap<>();
        String[] hostList = xQuantPPConfig.getHostPort();
        for (String hosts : hostList) {
            String[] hostAndPort = hosts.split(":");
            int status = ppService.test(hosts);
            String host =  hostAndPort[0];
            SysVisulationController(host);
            Map<String, String> result = runDistanceShell(COMMANDS);
            String message = disposeResultMessage(result);
            String[] menory = message.split("交换:");
            String[] swap = menory[1].split("系统磁盘状态:大小");
            String totalMsg = menory[0].replace("\r\n","").concat(swap[0].replace("\r\n",""));
            serveMsg.put(host,totalMsg);
        }
        return serveMsg;
    }

    //获取计算服务的状态
    @RequestMapping("/getServerStatus")
    @RequiresPermissions("monitor:cal:query")
    public List<ServeStatusVo> getServerStatus() {
        List<ServeStatusVo> statusVoList = new ArrayList<ServeStatusVo>();
        List<XQuantPPStubFactory> lst = ppService.getLst();
        for (XQuantPPStubFactory stubFactory : lst) {
            ServeStatusVo serveStatusVo = new ServeStatusVo();
            int status = ppService.test(MapUtil.getStr(stubFactory.getPp(), "address"));
            serveStatusVo.setAddress(MapUtil.getStr(stubFactory.getPp(), "address"));
            serveStatusVo.setStatus(status);
            statusVoList.add(serveStatusVo);
        }
        return statusVoList;
    }

    //重启计算服务
    @RequestMapping("/restartServer")
    @RequiresPermissions("monitor:cal:restart")
    public CommResponse restartServer(@RequestParam Map<String, Object> queryParams) {
        String address = (String) queryParams.get("address");
        int restartStatus = ppService.restart(address);
        if (restartStatus == 0){
            return CommResponse.success();
        }else if (restartStatus == 1){
            return CommResponse.failure("无此计算服务");
        }
        return CommResponse.failure("服务异常，，重启失败");
    }
}
